package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.OrderReport;
import com.sky.entity.Orders;
import com.sky.entity.TurnoverReport;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper {
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime time);

    void batchCancelOrdersByIds(List<Long> ids, Integer status, LocalDateTime cancelTime, String cancelReason);

    void batchSetStatusCompletedByIds(List<Long> ids, Integer status);

    Page<Orders> getByStatusAndUserId(Integer status, Long userId);

    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    Page<Orders> pageConditionSearch(LocalDateTime beginTime, LocalDateTime endTime, String number, String phone, Integer status);

    /**
     * 查询指定状态的订单数量
     * @param status
     * @return
     */
    @Select("select count(*) from orders where status = #{status}")
    Integer countOrderByStatus(Integer status);

    @Select("select sum(amount) from orders where status = #{status} and  date(order_time) = #{date}")
    Double getSumAmountByStatusAndOrderTime(Integer status, LocalDate date);

    /**
     * 根据订单状态和起始日期并按照下单日期分组统计每一天的营业额
     * @param status
     * @param begin
     * @param end
     * @return
     */
    List<TurnoverReport> getSumAmountGroupByOrderTime(Integer status, LocalDate begin, LocalDate end);

    /**
     * 根据下单日期和订单状态分组查询订单数
     * @param map
     * @return
     */
    List<OrderReport> countOrderByMap(Map map);

    /**
     * 根据订单状态查询数量
     * @param orders
     * @return
     */
    Integer getCountByStatus(Orders orders);

    /**
     * 查询指定时间段内销量排名前十的商品名称和数量
     * @param begin
     * @param end
     * @return
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDate begin, LocalDate end);

    /**
     * 根据map条件查询订单数
     * @param map
     * @return
     */
    Integer countByMap(Map map);

    /**
     * 根据map条件查询营业总额
     * @param map
     * @return
     */
    Double sumByMap(Map map);
}
